<template>
  <div class="mes-box">
    <div
      class="mes-item"
      :class="{
        active: item.open_id === index && item.open_id !== openid,
        ok: item.open_id === openid,
      }"
      v-for="item in chatList"
      :key="item.open_id"
      @mouseenter.stop="changeindex(item.open_id)"
      @mouseleave.stop="changeindex(-1)"
      @click="changeChat(item)"
    >
      <img
        style="width: 45px; height: 45px; border-radius: 50%; display: block"
        :src="item.that_avater"
        alt=""
      />
      <div class="mes-item-content">
        <div
          style="font-size: 14px; color: #333"
          :class="{ foncolor: item.open_id === openid }"
        >
          {{ item.that_notes ? item.that_notes : item.that_nickname }}
        </div>
        <div
          style="
            font-size: 12px;
            color: #565050;
            margin-top: 10px;
            width: 180px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            line-height: 12px;
            padding-bottom: 5px;
            box-sizing: border-box;
          "
          :class="{ foncolor: item.open_id === openid }"
        >
          {{ item.least_msg }}
        </div>
      </div>
      <div class="mes-item-time" :class="{ foncolor: item.open_id === openid }">
        {{ item.least_time }}
      </div>
      <div class="mes-icon" v-if="item.unread > 0">{{ item.unread }}</div>
    </div>
  </div>
</template>

<script>

export default {
  props: {
    chatList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      index: -1,
      openid: ''
    }
  },
  methods: {
    changeindex(index) {
      this.index = index
    },
    changeChat(item) {
      this.openid = item.open_id
      this.$emit('changeChat', item)
    },
    select(val) {
      this.openid = this.chatList.find(itme => {
        return itme.that_id === val
      }).open_id
    }
  }
}
</script>

<style lang="scss" scoped>
.mes-box {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  cursor: pointer;
  .active {
    background: #f5f5f5;
  }
  .ok {
    background: rgb(0, 153, 255) !important;
  }
  .mes-icon {
    width: 20px;
    height: 20px;
    background: red;
    border-radius: 50%;
    display: flex;
    margin-left: 10px;
    position: absolute;
    bottom: 15px;
    right: 18px;
    font-size: 12px;
    color: #fff;
    align-items: center;
    justify-content: center;
  }
  .foncolor {
    color: #fff !important;
  }
  .mes-item {
    width: 100%;
    height: 80px;
    padding: 18px;
    display: flex;
    position: relative;
    .mes-item-content {
      display: flex;
      flex-direction: column;
      margin-left: 20px;
      position: relative;
    }
    .mes-item-time {
      position: absolute;
      right: 20px;
      top: 20px;
      color: #999;
      font-size: 12px;
    }
  }
}
</style>